//
//  BHGameBannerCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/10.
//

import UIKit

class BHGameBannerCell: UICollectionViewCell {
    
    
    lazy var bannerView: BHCycleView = {
        let view = BHCycleView.init()
        view.backgroundColor = .color_f8f8f8
        view.collectionView.backgroundColor = .color_f8f8f8
        view.scrollDirection = .horizontal
        view.timerValue = 8
        view.leadingAndTralingSpace = 0
        view.lineSpace = 0
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    var banner: [BHCommonBannerModel] = [] {
        didSet {
            self.bannerView.sourceArray = banner
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.bannerView.addLayerCorner(8, 0, .white)
        self.contentView.addSubview(self.bannerView)
        self.bannerView.mas_makeConstraints { (make) in
            make?.left.equalTo()(self)?.offset()(16)
            make?.right.equalTo()(self)?.offset()(-16)
            make?.top.equalTo()(self)?.offset()(0)
            make?.bottom.equalTo()(self)?.offset()(-10)
        }
    }
    
    
    static var cellHeight: CGFloat {
        get {
            return CGFloat(130).radius
        }
    }
    
}
extension BHGameBannerCell: BHCycleViewDataSource,BHCycleViewDelegate {
    func cellForRow(_ indexPath: IndexPath, _ collectionView: UICollectionView,_ cycleView: UIView) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCommonBannerImageCell", for: indexPath) as! BHCommonBannerImageCell
        let model = self.banner[indexPath.item%self.banner.count]
        cell.bannerImageUrl = model.image_url
        cell.backgroundColor = .clear
        return cell
    }
    func sizeForItem(_ indexPath: IndexPath,_ cycleView: UIView) -> CGSize {
        return CGSize(width: UIScreen.width-32, height: CGFloat(138).radius)
    }
    
    func numberOfItem(_ cycleView: UIView) -> Int {
        return self.banner.count
    }
    func didSelectItem(_ index: Int,_ cycleView: UIView) {
        let model = self.banner[index]
        BHRouter.shared.bh_bannerClick(model: model)
    }
}
